package com.le.tester.leetcode;

import java.awt.*;
import java.util.Arrays;

/**
 * 描述：搜素插入位置
 */
public class SearchInsert {
    public int searchInsert(int[] nums, int target) {

        int result = -1;
        int length = nums.length;
        boolean contains = Arrays.stream(nums).anyMatch(o -> o == target);
        //该target在该数组中
        if (contains) {
            for (int i = 0; i < length; i++) {
                if (nums[i] == target) {
                    result = i;
                    return result;
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                if (target < nums[0]) {
                    return 0;
                } else if (target > nums[length - 1]) {
                    return length;
                } else {
                    //在中间的位置
                    if (target > nums[i] && target < nums[i + 1]) {
                        return i + 1;
                    }
                }
            }
        }
        //该target不在数组中
        return result;
    }

    public static void main(String[] args) {
        int[] ints = {1, 3, 5, 9};
        int target = 7;
        System.out.println(new SearchInsert().searchInsert(ints, target));

    }
}
